/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.serviportales.serviquality.managers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.serviportales.serviquality.dao.PlantillaDAO;
import com.serviportales.serviquality.exception.ServiqualityException;
import com.serviportales.serviquality.model.Plantilla;

@Service
@Transactional(rollbackFor = { ServiqualityException.class })
public class MgrPlantillas {


	@Autowired
	private PlantillaDAO plantillaDAO;
	@Autowired
	private MessageSource messageSource;

	public void grabarPlantilla(Plantilla plantilla) throws ServiqualityException {
		try {
			Plantilla plantillaFilter = plantillaDAO.selectByID(plantilla.getCodigo());
			if (plantillaFilter != null) {
				plantillaFilter.setDescripcion(plantilla.getDescripcion());
				plantillaFilter.setEstadoRegistro(plantilla.getEstadoRegistro());
				plantillaFilter.setNombre(plantilla.getNombre());				
				plantillaDAO.update(plantillaFilter);
			} else {
				plantillaDAO.insert(plantilla);
			}
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.GRABAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public void eliminarPlantilla(Plantilla plantilla) throws ServiqualityException {
		try {
			plantillaDAO.delete(plantilla);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.ELIMINAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public List<Plantilla> buscarPlantilla(Plantilla plantilla) throws ServiqualityException {
		try {
			return plantillaDAO.select(plantilla);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.BUSCAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}

	public List<Plantilla> buscarPlantilla(Plantilla plantilla, int maxResult) throws ServiqualityException {
		try {
			return plantillaDAO.select(plantilla, 0, maxResult);
		} catch (Exception exception) {
			throw new ServiqualityException(messageSource.getMessage(ServiqualityException.BUSCAR_ERROR, new String[] { exception.getMessage() }, LocaleContextHolder.getLocale()), exception);
		}
	}
}
